package com.suge.user.manager.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.suge.order.dao.UserBizOrderDAO;
import com.suge.order.dao.dataobject.UserBizOrderDO;
import com.suge.user.dao.UserDAO;
import com.suge.user.dao.dataobject.UserDO;
import com.suge.user.manager.UserManager;
import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.domain.User;
import com.taobao.api.request.IncrementCustomerPermitRequest;
import com.taobao.api.request.UserGetRequest;
import com.taobao.api.request.UserSellerGetRequest;
import com.taobao.api.response.IncrementCustomerPermitResponse;
import com.taobao.api.response.UserGetResponse;
import com.taobao.api.response.UserSellerGetResponse;

public class UserManagerImpl implements UserManager{

	private static final Logger logger = LoggerFactory.getLogger(UserManagerImpl.class);
	
	private static final String appKey="21430427";
	
	private static final String appSecret = "89fbda48de40e8833e8e0289919a4e06";
	
	private static final String tbUrl = "http://gw.api.taobao.com/router/rest";
	
	@Autowired
	private UserDAO userDAO;
	@Autowired
	private UserBizOrderDAO userBizOrderDAO;
	
	public UserDO getAppUserByUserId(long userId) {
		
		UserDO userDO =  userDAO.getAppUserByUserId(userId);
		if(userDO!=null){
			UserBizOrderDO userBizOrderDO = userBizOrderDAO.getUserBizOrderByUserId(userId);
			userDO.setValidNum(userBizOrderDO.getValidMessageCount());
			userDO.setHasUserNum(userBizOrderDO.getMessageSentCount());
		}
		
		return userDO;
	}

	public boolean updateUserInfo(UserDO appUserDO) {
		
		return userDAO.updateUserInfo(appUserDO);
	}

	public UserDAO getUserDAO() {
		return userDAO;
	}

	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}

	public UserDO getAppUser4Open(String openId,String openKey) {
	return null;
	}
	
	public UserDO getTaoBaoUserInfo(String sessionKey){
		
			TaobaoClient client = new DefaultTaobaoClient(tbUrl, appKey, appSecret);
	        UserSellerGetRequest req = new UserSellerGetRequest();
	        req.setFields("user_id,nick,avatar,email");
	        UserSellerGetResponse response;
	        UserDO userDO=null;
	        try {
	            response = client.execute(req,sessionKey); 
	            logger.error("result:"+response.getBody());
	            logger.error("nick:"+response.getUser().getNick());
	            UserDO userConvertDO = convertTOLocalUserDO(response.getUser());
	            /**
	             * 这里判断下该用户是否存在、如果不存在就插入一条
	             */
	            userDO =  userDAO.getAppUserByUserId(response.getUser().getUserId());
	            if(userDO==null){
	            	userConvertDO.setSessionKey(sessionKey);
	            	userDAO.insertUser(userConvertDO);
	            	userDO =  userConvertDO;
	            }
	            IncrementCustomerPermitRequest reqPermit=new IncrementCustomerPermitRequest();
		        reqPermit.setType("get,syn,notify");
		        reqPermit.setTopics("trade");
		        reqPermit.setStatus("all");
		        IncrementCustomerPermitResponse responsePermit = client.execute(reqPermit , sessionKey);
		        if(!responsePermit.isSuccess()){
		        	 logger.error("当前用户订阅消息不成功！"+userDO);
		        	 logger.error("当前用户订阅消息不成功de yuan yin:"+responsePermit.getBody());
		        	 logger.error("当前用户订阅消息不成功de yuan yin:"+responsePermit.getMsg());
		        	 logger.error("当前用户订阅消息不成功de yuan yin:"+responsePermit.getSubCode());
		        	 logger.error("当前用户订阅消息不成功de yuan yin:"+responsePermit.getSubMsg());
		        }
		        else{
		        	logger.error("当前用户订阅消息成功!!!!!!!!!!!!!!!!!!!!！");
		        }
	        } catch (ApiException e) {
	        	 logger.error(e.toString());
	        }
	        
	        return userDO;
	}
	
	private UserDO convertTOLocalUserDO(User user){
		
		UserDO userDO = new UserDO();
		userDO.setAppId(0);
		userDO.setAppName("taobao");
		userDO.seteMail(user.getEmail());
		userDO.setHeadPic(user.getAvatar());
		userDO.setUserId(user.getUserId());
		userDO.setUserNick(user.getNick());
		userDO.setPoints(0);
		userDO.setIsVip(0);
		userDO.setVipGrade(0);
		return userDO;
	}

	public void setUserBizOrderDAO(UserBizOrderDAO userBizOrderDAO) {
		this.userBizOrderDAO = userBizOrderDAO;
	}

	public UserBizOrderDAO getUserBizOrderDAO() {
		return userBizOrderDAO;
	}

	public UserDO getAppUserByUserNick(String userNick) {
		UserDO userDO =  userDAO.getAppUserByUserNick(userNick);
		UserBizOrderDO bizOrder = userBizOrderDAO.getUserBizOrderByUserNick(userNick);
		if(bizOrder!=null){
			userDO.setValidNum(bizOrder.getValidMessageCount());
		}
		return userDO;
	}
}
